package service

import (
	"context"
	"errors"
	"fmt"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/dao"
)

// 优惠券创建
func (s *Server) CouponCreate(_ context.Context, in *__.CouponCreateReq) (*__.CouponCreateResp, error) {
	// 校验参数
	if in.CouponType <= 0 || in.Price <= 0 || in.OrderId <= 0 {
		return nil, errors.New("参数错误")
	}
	// 校验优惠券类型
	if in.CouponType > 2 {
		return nil, errors.New("优惠券类型错误")
	}
	// 校验优惠券金额
	if in.Price > 100 || in.Price < 10 {
		return nil, errors.New("优惠券金额错误")
	}

	create, err := dao.CouponCreate(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.CouponCreateResp{
		Id: create.Id,
	}, nil
}
